/**
 * 
 */
package com.nervytech.tutorcruise.db.dao.mysql.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.nervytech.tutorcruise.db.dao.interfaces.ICourseMasterDAO;
import com.nervytech.tutorcruise.db.dao.model.CourseMaster;

/**
 * @author ADMIN
 * 
 */
@Repository("courseMasterDAO")
public class CourseMasterDAO extends JdbcDaoSupport implements ICourseMasterDAO {

	@Autowired
	CourseMasterDAO(DataSource dataSource) {
		setDataSource(dataSource);
	}

	@Override
	public void saveCourseOverview(CourseMaster courseMaster) {

		Map<String, Object> argsMap = new HashMap<String, Object>();
		
		System.out.println("LEVELLLLLLLLLLLL issssssssssss "+courseMaster.getLevel());
		argsMap.put("user_id", courseMaster.getUserId());
		argsMap.put("title", courseMaster.getTitle());
		argsMap.put("category", courseMaster.getCategory());
		argsMap.put("level", courseMaster.getLevel());
		argsMap.put("overview", courseMaster.getOverview());
		argsMap.put("objective", courseMaster.getObjective());
		argsMap.put("prerequisite", courseMaster.getPrerequisite());
		argsMap.put("audience", courseMaster.getAudience());
		argsMap.put("tagged_skills", courseMaster.getTaggedSkills());
		argsMap.put("created_time", courseMaster.getCreatedTime());
		argsMap.put("updated_time", courseMaster.getUpdatedTime());

		long courseMasterId = new SimpleJdbcInsert(getJdbcTemplate())
				.withTableName("course_master")
				.usingGeneratedKeyColumns("course_master_id")
				.executeAndReturnKey(argsMap).longValue();

		courseMaster.setCourseMasterId(courseMasterId);

	}

	@Override
	public void updateCourseOverview(CourseMaster courseMaster) {

		String saveUserQuery = "update course_master set title=?,category=?,level=?,overview=?,objective=?,prerequisite=?,audience=?,"
				+ " tagged_skills=?,created_time=?,updated_time=? where course_master_id=? and user_id=?";

		Object[] parms = { courseMaster.getTitle(), courseMaster.getCategory(),
				courseMaster.getLevel(), courseMaster.getOverview(),
				courseMaster.getObjective(), courseMaster.getPrerequisite(),
				courseMaster.getAudience(), courseMaster.getTaggedSkills(),
				courseMaster.getCreatedTime(), courseMaster.getUpdatedTime(),
				courseMaster.getCourseMasterId(), courseMaster.getUserId() };

		getJdbcTemplate().update(saveUserQuery, parms);

	}

	@Override
	public void updateCourseAgenda(CourseMaster courseMaster) {
		String saveUserQuery = "update course_master set agenda=?,trainer_details=?,notes=?,"
				+ " updated_time=? where course_master_id=? and user_id=?";

		Object[] parms = { courseMaster.getAgenda(),
				courseMaster.getTrainerDetails(), courseMaster.getNotes(),
				courseMaster.getUpdatedTime(),
				courseMaster.getCourseMasterId(), courseMaster.getUserId() };

		getJdbcTemplate().update(saveUserQuery, parms);

	}

	@Override
	public List<CourseMaster> getCourseOverview(long userId, long courseId) {

		RowMapper<CourseMaster> mapper = new RowMapper<CourseMaster>() {
			public CourseMaster mapRow(ResultSet rs, int rowNum)
					throws SQLException {

				CourseMaster courceMaster = new CourseMaster();
				courceMaster.setTitle(rs.getString("title"));
				courceMaster.setCategory(rs.getInt("category"));
				courceMaster.setLevel(rs.getInt("level"));
				courceMaster.setOverview(rs.getString("overview"));
				courceMaster.setObjective(rs.getString("objective"));
				courceMaster.setPrerequisite(rs.getString("prerequisite"));
				courceMaster.setAudience(rs.getString("audience"));
				courceMaster.setTaggedSkills(rs.getString("tagged_skills"));

				return courceMaster;
			}
		};
		String selectUserQuery = "SELECT title,category,level,overview,objective,prerequisite,audience,tagged_skills"
				+ " FROM course_master WHERE course_master_id=? and user_id=?";

		logger.debug("Select user Query : " + selectUserQuery);

		return getJdbcTemplate().query(selectUserQuery, mapper);
	}

	@Override
	public List<CourseMaster> getCourseAgenda(long userId, long courseMasterId) {
		
		RowMapper<CourseMaster> mapper = new RowMapper<CourseMaster>() {
			public CourseMaster mapRow(ResultSet rs, int rowNum)
					throws SQLException {

				CourseMaster courceMaster = new CourseMaster();
				courceMaster.setAgenda(rs.getString("agenda"));
				courceMaster.setTrainerDetails(rs.getString("trainer_details"));
				courceMaster.setNotes(rs.getString("notes"));

				return courceMaster;
			}
		};
		String selectUserQuery = "SELECT agenda,trainer_details,notes"
				+ " FROM course_master WHERE course_master_id=? and user_id=?";

		logger.debug("Select user Query : " + selectUserQuery);

		return getJdbcTemplate().query(selectUserQuery, mapper);
	}

}
